﻿using Report_Dal.Web;
using Report_IDal.Web;
using Report_IService.Web;
using Report_Model.Basic;
using Report_Model.Db;
using Report_Model.Web;
using Report_Service.Basic;
using Report_Unity.Basic;
using Report_Unity.Unitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Report_Service.Web
{
    public class T_EmployeeService : BaseService<T_Employee>, IT_EmployeeService
    {
        private IT_EmployeeDal T_EmployeeDal = new T_EmployeeDal(ConfigSource.tab_web);

        public override void SetCurrentDal()
        {
            base.BaseDal = T_EmployeeDal;
        }

        public ResultInfo AddEmployee(string hid,string cid,string name,string idcard,string phone,string userid)
        {
            if (string.IsNullOrEmpty(hid))
                return CodeStatus.error_201.GetStatus($"{nameof(hid)}必传");
            if (string.IsNullOrEmpty(cid))
                return CodeStatus.error_201.GetStatus($"{nameof(cid)}必传");
            if (string.IsNullOrEmpty(name))
                return CodeStatus.error_201.GetStatus($"{nameof(name)}必传");
            if (string.IsNullOrEmpty(idcard))
                return CodeStatus.error_201.GetStatus($"{nameof(idcard)}必传");
            if (string.IsNullOrEmpty(phone))
                return CodeStatus.error_201.GetStatus($"{nameof(phone)}必传");
            if (string.IsNullOrEmpty(userid))
                return CodeStatus.error_201.GetStatus($"{nameof(userid)}必传");

            if(!VerifyHelper.IsIDCard(idcard))
                return CodeStatus.error_201.GetStatus($"身份证号不合法");

            if (!VerifyHelper.IsIDCard(phone))
                return CodeStatus.error_201.GetStatus($"手机号不合法");


            T_Employee employee = GetT_Employee(hid,cid,name,idcard,phone,userid);

            if (!Add(employee))
                return CodeStatus.error_301.GetStatus("添加失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo EditEmployee(string id,string hid,string cid,string name,string idcard,string phone,string userid)
        {
            if (string.IsNullOrEmpty(id))
                return CodeStatus.error_201.GetStatus($"{nameof(id)}必传");
            if (string.IsNullOrEmpty(hid))
                return CodeStatus.error_201.GetStatus($"{nameof(hid)}必传");
            if (string.IsNullOrEmpty(cid))
                return CodeStatus.error_201.GetStatus($"{nameof(cid)}必传");
            if (string.IsNullOrEmpty(name))
                return CodeStatus.error_201.GetStatus($"{nameof(name)}必传");
            if (string.IsNullOrEmpty(idcard))
                return CodeStatus.error_201.GetStatus($"{nameof(idcard)}必传");
            if (string.IsNullOrEmpty(phone))
                return CodeStatus.error_201.GetStatus($"{nameof(phone)}必传");
            if (string.IsNullOrEmpty(userid))
                return CodeStatus.error_201.GetStatus($"{nameof(userid)}必传");

            T_Employee employee = T_EmployeeDal.Get(a => a.id == id && a.hospital_id == hid && a.company_id == cid).FirstOrDefault();
            if (employee == null)
                return CodeStatus.error_202.GetStatus("无信息");

            employee.name = name;
            employee.Initials = PinYinUtils.GetFirstSpell(name);
            employee.idcard = idcard;
            employee.phone = phone;
            employee.birthday = IdCardUtils.GetBirthday(idcard);
            employee.sex = IdCardUtils.GetSex(idcard);
            employee.up_userid = userid;
            employee.up_time = DateTime.Now;

            if (!Up(employee))
                return CodeStatus.error_301.GetStatus("修改失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo DelEmployee(string id,string hid,string cid)
        {
            if (string.IsNullOrEmpty(id))
                return CodeStatus.error_201.GetStatus($"{nameof(id)}必传");
            if (string.IsNullOrEmpty(hid))
                return CodeStatus.error_201.GetStatus($"{nameof(hid)}必传");
            if (string.IsNullOrEmpty(cid))
                return CodeStatus.error_201.GetStatus($"{nameof(cid)}必传");

            T_Employee employee = Get(a => a.id == id && a.hospital_id == hid && a.company_id == cid).FirstOrDefault();
            if (employee == null)
                return CodeStatus.error_202.GetStatus("无信息");

            if (!Up(employee))
                return CodeStatus.error_301.GetStatus("删除失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo MoveEmployee(string id, string hid, string cid,string dirc)
        {
            if (string.IsNullOrEmpty(id))
                return CodeStatus.error_201.GetStatus($"{nameof(id)}必传");
            if (string.IsNullOrEmpty(hid))
                return CodeStatus.error_201.GetStatus($"{nameof(hid)}必传");
            if (string.IsNullOrEmpty(cid))
                return CodeStatus.error_201.GetStatus($"{nameof(cid)}必传");

            T_Employee employee = GetAsNoTracking(a => a.id == id && a.hospital_id == hid && a.company_id == cid).FirstOrDefault();
            if (employee == null)
                return CodeStatus.error_202.GetStatus("无信息");

            ResultInfo res = MoveObj(a => a.hospital_id == hid && a.company_id == cid, a => a.id == cid && a.hospital_id == hid && a.company_id == cid, a => a.sort, dirc);
            return res;
        }

        public ResultInfo GetListEmployee(string page, string size,string name,string idcard,string phone,string sex)
        {
            int pageIndex, pageSize,count;
            if (!int.TryParse(page, out pageIndex))
                pageIndex = 1;

            if (!int.TryParse(size, out pageSize))
                pageSize = 50;

            var iQuery = GetAsNoTracking(a => true);

            if (!string.IsNullOrEmpty(name))
            {
                if (PinYinUtils.ContainChinese(name))
                    iQuery = iQuery.Where(a => a.name.StartsWith(name));
                else
                    iQuery = iQuery.Where(a => a.Initials.StartsWith(name));
            }

            if (!string.IsNullOrEmpty(idcard))
                iQuery = iQuery.Where(a => a.idcard == idcard);

            if (!string.IsNullOrEmpty(phone))
                iQuery = iQuery.Where(a => a.phone == phone);

            if (!string.IsNullOrEmpty(sex))
                iQuery = iQuery.Where(a => a.sex == sex);

            count = iQuery.Count();
            List<T_Employee> listEmployee = iQuery.OrderBy(a => a.sort).ThenBy(a=>a.id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            List<object> listReslt = new List<object>();
            foreach (T_Employee employee in listEmployee)
            {
                object obj = new
                {
                    id = employee.id,
                    name = employee.name,
                    idcard = StringUtils.ReplaceWithSpecialChar(idcard),
                    phone = StringUtils.ReplaceWithSpecialChar(phone),
                    sex = employee.sex,
                    birthday = employee.birthday,
                    is_leader = employee.is_leader,
                };

                listReslt.Add(obj);
            }

            ResultInfo res = CodeStatus.success.GetStatus();
            res.data = new { count = count,list = listReslt };

            return res;
        }

        public ResultInfo ChanageLeaderType(string id,string hid,string cid)
        {
            if (string.IsNullOrEmpty(id))
                return CodeStatus.error_201.GetStatus($"{nameof(id)}必传");
            if (string.IsNullOrEmpty(hid))
                return CodeStatus.error_201.GetStatus($"{nameof(hid)}必传");
            if (string.IsNullOrEmpty(cid))
                return CodeStatus.error_201.GetStatus($"{nameof(cid)}必传");

            T_Employee employee = Get(a => a.id == id && a.hospital_id == hid && a.company_id == cid).FirstOrDefault();
            if(employee == null)
                return CodeStatus.error_202.GetStatus("无信息");

            int general = EmployeeTypeEnum.general.GetHashCode();
            int administrator = EmployeeTypeEnum.administrator.GetHashCode();
            if (employee.is_leader == general)
                employee.is_leader = administrator;
            else
                employee.is_leader = general;

            if (!Up(employee))
                return CodeStatus.error_301.GetStatus("修改失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo ImportExcel(HttpPostedFile file,string hid,string cid,string userid)
        {
            if (string.IsNullOrEmpty(hid))
                return CodeStatus.error_201.GetStatus($"{nameof(hid)}必传");
            if (string.IsNullOrEmpty(cid))
                return CodeStatus.error_201.GetStatus($"{nameof(cid)}必传");
            if (string.IsNullOrEmpty(userid))
                return CodeStatus.error_201.GetStatus($"{nameof(userid)}必传");
            if(file == null)
                return CodeStatus.error_201.GetStatus($"{nameof(file)}必传");

            List<object> listError = new List<object>();
            List<ExcelEmployeeModel> list = ExcelUtils.ReadContent<ExcelEmployeeModel>(file);

            foreach (ExcelEmployeeModel emp in list)
            {
                if(string.IsNullOrWhiteSpace(emp.idcard))
                {
                    object obj = new { name = emp.name,idcard = emp.idcard,phone = emp.phone,error = "身份证号为空"};
                    listError.Add(obj);
                    continue;
                }

                if (!VerifyHelper.IsIDCard(emp.idcard))
                {
                    object obj = new { name = emp.name, idcard = emp.idcard, phone = emp.phone, error = "身份证号不合法" };
                    listError.Add(obj);
                    continue;
                }

                if (string.IsNullOrWhiteSpace(emp.name))
                {
                    object obj = new { name = emp.name, idcard = emp.idcard, phone = emp.phone, error = "姓名为空" };
                    listError.Add(obj);
                    continue;
                }

                if(string.IsNullOrWhiteSpace(emp.phone))
                {
                    object obj = new { name = emp.name, idcard = emp.idcard, phone = emp.phone, error = "手机号为空" };
                    listError.Add(obj);
                    continue;
                }

                if (!VerifyHelper.IsPhone(emp.phone))
                {
                    object obj = new { name = emp.name, idcard = emp.idcard, phone = emp.phone, error = "手机号不合法" };
                    listError.Add(obj);
                    continue;
                }

                T_Employee employee = GetAsNoTracking(a => a.hospital_id == hid && a.company_id == cid && a.idcard == emp.idcard && a.phone == emp.phone && a.name == emp.name).FirstOrDefault();
                if (employee != null)
                {
                    object obj = new { name = emp.name, idcard = emp.idcard, phone = emp.phone, error = "已存在" };
                    listError.Add(obj);
                    continue;
                }

                employee = GetT_Employee(hid,cid,emp.name,emp.idcard,emp.phone, userid);
                if (!Add(employee))
                    continue;
            }

            ResultInfo res = CodeStatus.success.GetStatus();
            if (listError.Count > 0)
            {
                res = CodeStatus.error_251.GetStatus();
                res.data = new { list = listError };
            }

            return res;
        }

        public byte[] GetExcelTemplate()
        {
            return ExcelUtils.GetExcelTemlate<ExcelEmployeeModel>();
        }

        private T_Employee GetT_Employee(string hid, string cid, string name, string idcard, string phone, string userid)
        {
            T_Employee employee = new T_Employee();
            employee.id = GetNextId(a=>a.id,true,true,true);
            employee.hospital_id = hid;
            employee.company_id = cid;
            employee.name = name;
            employee.idcard = idcard;
            employee.phone = phone;
            employee.birthday = IdCardUtils.GetBirthday(idcard);
            employee.sex = IdCardUtils.GetSex(idcard);
            employee.create_userid = userid;
            employee.create_time = DateTime.Now;
            employee.Initials = PinYinUtils.GetFirstSpell(name);
            employee.is_leader = EmployeeTypeEnum.general.GetHashCode();
            employee.sort = GetNextSort(a => a.hospital_id == hid && a.company_id == cid, a => a.sort);
            return employee;
        }
    }
}
